import IndexApi from '@/api/index.js'
import {defaultLang} from "@/common/config.js";
export default {
    state: {
       timestamp:123456,
	   translates:[],
	   trans:{},
	   defaultLang:defaultLang
    },
    getters: {
       timeString(state) {//时间戳转换后的时间
           var date = new Date(state.timestamp);
           var year = date.getFullYear();
           var mon  = date.getMonth()+1;
           var day  = date.getDate();
           var hours = date.getHours();
           var minu = date.getMinutes();
           var sec = date.getSeconds();
           var trMon = mon<10 ? '0'+mon : mon
           var trDay = day<10 ? '0'+day : day
           return year+'-'+trMon+'-'+trDay+" "+hours+":"+minu+":"+sec;
       }
    },
    mutations: {
        updateTime(state){
			//更新当前时间戳
            state.timestamp = Date.now()
        },
		setTranslates(state,translates){
			state.translates=translates
			translates.forEach(item=>{
				state.trans[item.key]=item.value
			})
		},
		setDefaultLang(state,defaultLang){
			state.defaultLang=defaultLang
		}
    },
    actions: {
		//通过actions提交更新数据
    	test({commit}){
    		commit('updateTime')
    	},
		getTranslateList(context){
			return new Promise(resolve => {
				
				IndexApi.getTranslates().then(res=>{
					if(res.code==200){
						context.commit('setTranslates',res.data)
						resolve(res.data)
					}
				}).catch(e=>{})
			})
		}
    }
}